package api

import (
	"net/http"

	"example.com/rest/example/dao"
	"example.com/rest/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configPlaylistTrackRouter(router *httprouter.Router) {
	router.GET("/playlisttrack", GetAllPlaylistTrack)
	router.POST("/playlisttrack", AddPlaylistTrack)
	router.GET("/playlisttrack/:argPlaylistID", GetPlaylistTrack)
	router.PUT("/playlisttrack/:argPlaylistID", UpdatePlaylistTrack)
	router.DELETE("/playlisttrack/:argPlaylistID", DeletePlaylistTrack)
}

func configGinPlaylistTrackRouter(router gin.IRoutes) {
	router.GET("/playlisttrack", ConverHttprouterToGin(GetAllPlaylistTrack))
	router.POST("/playlisttrack", ConverHttprouterToGin(AddPlaylistTrack))
	router.GET("/playlisttrack/:argPlaylistID", ConverHttprouterToGin(GetPlaylistTrack))
	router.PUT("/playlisttrack/:argPlaylistID", ConverHttprouterToGin(UpdatePlaylistTrack))
	router.DELETE("/playlisttrack/:argPlaylistID", ConverHttprouterToGin(DeletePlaylistTrack))
}

// GetAllPlaylistTrack is a function to get a slice of record(s) from playlist_track table in the main database
// @Summary Get list of PlaylistTrack
// @Tags PlaylistTrack
// @Description GetAllPlaylistTrack is a handler to get a slice of record(s) from playlist_track table in the main database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.PlaylistTrack}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /playlisttrack [get]
// http "http://localhost:8080/playlisttrack?page=0&pagesize=20" X-Api-User:user123
func GetAllPlaylistTrack(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "playlist_track", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllPlaylistTrack(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetPlaylistTrack is a function to get a single record from the playlist_track table in the main database
// @Summary Get record from table PlaylistTrack by  argPlaylistID
// @Tags PlaylistTrack
// @ID argPlaylistID
// @Description GetPlaylistTrack is a function to get a single record from the playlist_track table in the main database
// @Accept  json
// @Produce  json
// @Param  argPlaylistID path int true "PlaylistId"
// @Success 200 {object} model.PlaylistTrack
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /playlisttrack/{argPlaylistID} [get]
// http "http://localhost:8080/playlisttrack/1" X-Api-User:user123
func GetPlaylistTrack(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argPlaylistID, err := parseInt32(ps, "argPlaylistID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "playlist_track", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetPlaylistTrack(ctx, argPlaylistID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddPlaylistTrack add to add a single record to playlist_track table in the main database
// @Summary Add an record to playlist_track table
// @Description add to add a single record to playlist_track table in the main database
// @Tags PlaylistTrack
// @Accept  json
// @Produce  json
// @Param PlaylistTrack body model.PlaylistTrack true "Add PlaylistTrack"
// @Success 200 {object} model.PlaylistTrack
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /playlisttrack [post]
// echo '{"playlist_id": 42,"track_id": 86}' | http POST "http://localhost:8080/playlisttrack" X-Api-User:user123
func AddPlaylistTrack(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	playlisttrack := &model.PlaylistTrack{}

	if err := readJSON(r, playlisttrack); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := playlisttrack.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	playlisttrack.Prepare()

	if err := playlisttrack.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "playlist_track", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	playlisttrack, _, err = dao.AddPlaylistTrack(ctx, playlisttrack)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, playlisttrack)
}

// UpdatePlaylistTrack Update a single record from playlist_track table in the main database
// @Summary Update an record in table playlist_track
// @Description Update a single record from playlist_track table in the main database
// @Tags PlaylistTrack
// @Accept  json
// @Produce  json
// @Param  argPlaylistID path int true "PlaylistId"
// @Param  PlaylistTrack body model.PlaylistTrack true "Update PlaylistTrack record"
// @Success 200 {object} model.PlaylistTrack
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /playlisttrack/{argPlaylistID} [put]
// echo '{"playlist_id": 42,"track_id": 86}' | http PUT "http://localhost:8080/playlisttrack/1"  X-Api-User:user123
func UpdatePlaylistTrack(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argPlaylistID, err := parseInt32(ps, "argPlaylistID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	playlisttrack := &model.PlaylistTrack{}
	if err := readJSON(r, playlisttrack); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := playlisttrack.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	playlisttrack.Prepare()

	if err := playlisttrack.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "playlist_track", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	playlisttrack, _, err = dao.UpdatePlaylistTrack(ctx,
		argPlaylistID,
		playlisttrack)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, playlisttrack)
}

// DeletePlaylistTrack Delete a single record from playlist_track table in the main database
// @Summary Delete a record from playlist_track
// @Description Delete a single record from playlist_track table in the main database
// @Tags PlaylistTrack
// @Accept  json
// @Produce  json
// @Param  argPlaylistID path int true "PlaylistId"
// @Success 204 {object} model.PlaylistTrack
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /playlisttrack/{argPlaylistID} [delete]
// http DELETE "http://localhost:8080/playlisttrack/1" X-Api-User:user123
func DeletePlaylistTrack(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argPlaylistID, err := parseInt32(ps, "argPlaylistID")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "playlist_track", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeletePlaylistTrack(ctx, argPlaylistID)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
